package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.DatePropertyEditor;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.form.CustomerLimitForm;
import com.feeyin.radar.crm.model.Linkman;
import com.feeyin.radar.crm.model.QueryLinkman;
import com.feeyin.radar.crm.service.ContractService;
import com.feeyin.radar.crm.service.CustomerService;
import com.feeyin.radar.crm.service.LinkmanService;
import com.feeyin.radar.crm.service.LinkmanUpdateHistoryService;
import com.feeyin.radar.crm.service.SaleRecordService;
import com.feeyin.radar.sys.service.AssessmentTypeService;
import com.feeyin.radar.sys.service.SaleStageSetService;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class LinkmanController extends MultiActionController
{
  private LinkmanService linkmanService;
  private CustomerService customerService;
  private LinkmanUpdateHistoryService linkmanUpdateHistoryService;
  private AssessmentTypeService assessmentTypeService;
  private ContractService contractService;
  private SaleStageSetService saleStageSetService;
  private SaleRecordService saleRecordService;
  private String addLinkman;
  private String updateLinkman;
  private String updateLinkmanInfo;
  private String selectLinkmanDept;
  private String selectLinkmanDuty;
  private String linkmanInfo;
  private String linkmanInfoDetail;
  private String linkmanUpdateHistory;
  private String linkmanUpdateHistoryDetail;
  private String deptCustomerLinkMan;
  private String ownCustomerLinkMan;
  private String advancedQueryLinkMan;
  private String addLinkmanOther;

  public SaleRecordService getSaleRecordService()
  {
    return this.saleRecordService;
  }

  public void setSaleRecordService(SaleRecordService saleRecordService) {
    this.saleRecordService = saleRecordService;
  }

  public AssessmentTypeService getAssessmentTypeService() {
    return this.assessmentTypeService;
  }

  public void setAssessmentTypeService(AssessmentTypeService assessmentTypeService) {
    this.assessmentTypeService = assessmentTypeService;
  }

  public ContractService getContractService() {
    return this.contractService;
  }

  public void setContractService(ContractService contractService) {
    this.contractService = contractService;
  }

  public SaleStageSetService getSaleStageSetService() {
    return this.saleStageSetService;
  }

  public void setSaleStageSetService(SaleStageSetService saleStageSetService) {
    this.saleStageSetService = saleStageSetService;
  }

  public String getAddLinkmanOther()
  {
    return this.addLinkmanOther;
  }

  public void setAddLinkmanOther(String addLinkmanOther) {
    this.addLinkmanOther = addLinkmanOther;
  }

  public String getAdvancedQueryLinkMan() {
    return this.advancedQueryLinkMan;
  }

  public void setAdvancedQueryLinkMan(String advancedQueryLinkMan) {
    this.advancedQueryLinkMan = advancedQueryLinkMan;
  }

  public String getOwnCustomerLinkMan() {
    return this.ownCustomerLinkMan;
  }

  public void setOwnCustomerLinkMan(String ownCustomerLinkMan) {
    this.ownCustomerLinkMan = ownCustomerLinkMan;
  }

  public String getDeptCustomerLinkMan() {
    return this.deptCustomerLinkMan;
  }

  public void setDeptCustomerLinkMan(String deptCustomerLinkMan) {
    this.deptCustomerLinkMan = deptCustomerLinkMan;
  }

  public LinkmanService getLinkmanService() {
    return this.linkmanService;
  }

  public void setLinkmanService(LinkmanService linkmanService) {
    this.linkmanService = linkmanService;
  }

  public CustomerService getCustomerService() {
    return this.customerService;
  }

  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }

  public LinkmanUpdateHistoryService getLinkmanUpdateHistoryService() {
    return this.linkmanUpdateHistoryService;
  }

  public void setLinkmanUpdateHistoryService(LinkmanUpdateHistoryService linkmanUpdateHistoryService)
  {
    this.linkmanUpdateHistoryService = linkmanUpdateHistoryService;
  }

  public String getAddLinkman() {
    return this.addLinkman;
  }

  public void setAddLinkman(String addLinkman) {
    this.addLinkman = addLinkman;
  }

  public String getUpdateLinkman() {
    return this.updateLinkman;
  }

  public void setUpdateLinkman(String updateLinkman) {
    this.updateLinkman = updateLinkman;
  }

  public String getUpdateLinkmanInfo() {
    return this.updateLinkmanInfo;
  }

  public void setUpdateLinkmanInfo(String updateLinkmanInfo) {
    this.updateLinkmanInfo = updateLinkmanInfo;
  }

  public String getSelectLinkmanDept() {
    return this.selectLinkmanDept;
  }

  public void setSelectLinkmanDept(String selectLinkmanDept) {
    this.selectLinkmanDept = selectLinkmanDept;
  }

  public String getSelectLinkmanDuty() {
    return this.selectLinkmanDuty;
  }

  public void setSelectLinkmanDuty(String selectLinkmanDuty) {
    this.selectLinkmanDuty = selectLinkmanDuty;
  }

  public String getLinkmanInfo() {
    return this.linkmanInfo;
  }

  public void setLinkmanInfo(String linkmanInfo) {
    this.linkmanInfo = linkmanInfo;
  }

  public String getLinkmanInfoDetail() {
    return this.linkmanInfoDetail;
  }

  public void setLinkmanInfoDetail(String linkmanInfoDetail) {
    this.linkmanInfoDetail = linkmanInfoDetail;
  }

  public String getLinkmanUpdateHistory() {
    return this.linkmanUpdateHistory;
  }

  public void setLinkmanUpdateHistory(String linkmanUpdateHistory) {
    this.linkmanUpdateHistory = linkmanUpdateHistory;
  }

  public String getLinkmanUpdateHistoryDetail() {
    return this.linkmanUpdateHistoryDetail;
  }

  public void setLinkmanUpdateHistoryDetail(String linkmanUpdateHistoryDetail) {
    this.linkmanUpdateHistoryDetail = linkmanUpdateHistoryDetail;
  }

  public ModelAndView preAddLinkman(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("sign", request.getParameter("sign"));
    map.put("result", request.getParameter("result"));

    String customerId = request.getParameter("customerId");
    if (!StringUtils.isEmpty(customerId)) {
      map.put("customer", this.customerService.getCustomerById(Integer.valueOf(Integer.parseInt(customerId))));
      map.put("records", this.linkmanService.getLinkmanInfoRecords());
      map.put("ranges", this.linkmanService.getLinkmanDictionaryRanges());
    }

    return new ModelAndView(getAddLinkman(), map);
  }
  public ModelAndView preAddLinkmanOther(HttpServletRequest request, HttpServletResponse response) {
    Map map = new HashMap();
    map.put("sign", request.getParameter("sign"));
    map.put("result", request.getParameter("result"));
    map.put("records", this.linkmanService.getLinkmanInfoRecords());
    map.put("ranges", this.linkmanService.getLinkmanDictionaryRanges());
    return new ModelAndView(getAddLinkmanOther(), map);
  }

  public ModelAndView addLinkman(HttpServletRequest request, HttpServletResponse response, Linkman linkman) {
    boolean result = false;
    int success = 0;
    Integer customerId = Integer.valueOf(0);
    String sign = request.getParameter("sign");

    if (linkman != null) {
      customerId = linkman.getCustomerId();
      result = this.linkmanService.insertLinkman(linkman);
      if (result) success = 1;
    }

    return new ModelAndView(new RedirectView("./linkman.do?action=preAddLinkman&result=" + success + "&customerId=" + customerId + "&sign=" + sign));
  }

  public ModelAndView preUpdateLinkman(HttpServletRequest request, HttpServletResponse response)
  {
    String customerId = request.getParameter("customerId");
    Map map = new HashMap();
    map.put("sign", request.getParameter("sign"));
    map.put("result", request.getParameter("result"));
    map.put("id", request.getParameter("id"));

    if (!StringUtils.isEmpty(customerId)) {
      map.put("customer", this.customerService.getCustomerById(Integer.valueOf(Integer.parseInt(customerId))));
      map.put("linkmansAndDepts", this.linkmanService.getLinkmanAndDepts(Integer.valueOf(Integer.parseInt(customerId))));
    }
    return new ModelAndView(getUpdateLinkman(), map);
  }

  public ModelAndView preUpdateLinkmanInfo(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    String customerId = request.getParameter("customerId");
    String id = request.getParameter("id");
    map.put("sign", request.getParameter("sign"));
    if (!StringUtils.isEmpty(customerId)) {
      map.put("records", this.linkmanService.getLinkmanInfoRecords());
      map.put("ranges", this.linkmanService.getLinkmanDictionaryRanges());
      map.put("customer", this.customerService.getCustomerById(Integer.valueOf(Integer.parseInt(customerId))));

      if (!StringUtils.isEmpty(id)) {
        map.put("linkman", this.linkmanService.getLinkman(Integer.valueOf(Integer.parseInt(id))));
      }
    }

    return new ModelAndView(getUpdateLinkmanInfo(), map);
  }

  public ModelAndView updateLinkmanInfo(HttpServletRequest request, HttpServletResponse response, Linkman linkman)
  {
    boolean result = false;
    int success = 0;
    Integer customerId = Integer.valueOf(0);
    Integer id = Integer.valueOf(0);
    String sign = request.getParameter("sign");

    if ((linkman != null) && (linkman.getId() != null)) {
      customerId = linkman.getCustomerId();
      id = linkman.getId();
      result = this.linkmanService.updateLinkman(linkman);
      if (result) success = 1;
    }

    return new ModelAndView(new RedirectView("./linkman.do?action=preUpdateLinkman&result=" + success + "&customerId=" + customerId + "&id=" + id + "&sign=" + sign));
  }

  public ModelAndView deleteLinkman(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    String sign = request.getParameter("sign");
    String customerId = request.getParameter("customerId");
    String id = request.getParameter("id");
    if (!StringUtils.isEmpty(id)) {
      result = this.linkmanService.deleteLinkman(Integer.valueOf(Integer.parseInt(id)));
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./customerManage.do?action=customerDetail&customerId=" + customerId + "&sign=" + sign + "&result=" + success));
  }

  public ModelAndView selectLinkmanDept(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("ranges", this.linkmanService.getLinkmanDictionaryRanges());

    return new ModelAndView(getSelectLinkmanDept(), map);
  }

  public ModelAndView selectLinkmanDuty(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("ranges", this.linkmanService.getLinkmanDictionaryRanges());

    return new ModelAndView(getSelectLinkmanDuty(), map);
  }

  public ModelAndView linkmanInfo(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    String customerId = request.getParameter("customerId");
    map.put("sign", request.getParameter("sign"));

    if (!StringUtils.isEmpty(customerId)) {
      map.put("customer", this.customerService.getCustomerById(Integer.valueOf(Integer.parseInt(customerId))));
      map.put("linkmansAndDepts", this.linkmanService.getLinkmanAndDepts(Integer.valueOf(Integer.parseInt(customerId))));
    }

    return new ModelAndView(getLinkmanInfo(), map);
  }

  public ModelAndView linkmanInfoDetail(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    String customerId = request.getParameter("customerId");
    map.put("sign", request.getParameter("sign"));

    String id = request.getParameter("id");
    if (!StringUtils.isEmpty(customerId)) {
      map.put("records", this.linkmanService.getLinkmanInfoRecords());
      map.put("ranges", this.linkmanService.getLinkmanDictionaryRanges());
      map.put("customer", this.customerService.getCustomerById(Integer.valueOf(Integer.parseInt(customerId))));
    }
    if (!StringUtils.isEmpty(id)) {
      map.put("linkman", this.linkmanService.getLinkman(Integer.valueOf(Integer.parseInt(id))));
    }

    return new ModelAndView(getLinkmanInfoDetail(), map);
  }

  public ModelAndView linkmanUpdateHistory(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("result", request.getParameter("result"));
    String customerId = request.getParameter("customerId");
    map.put("customerId", customerId);
    map.put("sign", request.getParameter("sign"));

    String linkmanId = request.getParameter("id");
    map.put("linkmanId", linkmanId);
    if (!StringUtils.isEmpty(customerId)) {
      map.put("customer", this.customerService.getCustomerById(Integer.valueOf(Integer.parseInt(customerId))));
      map.put("linkmansAndDepts", this.linkmanService.getLinkmanAndDepts(Integer.valueOf(Integer.parseInt(customerId))));
    }

    return new ModelAndView(getLinkmanUpdateHistory(), map);
  }

  public ModelAndView linkmanUpdateHistoryDetail(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    String customerId = request.getParameter("customerId");
    map.put("customerId", customerId);
    map.put("sign", request.getParameter("sign"));

    String linkmanId = request.getParameter("id");
    map.put("linkmanId", linkmanId);
    if (!StringUtils.isEmpty(customerId)) {
      map.put("customer", this.customerService.getCustomerById(Integer.valueOf(Integer.parseInt(customerId))));

      if (!StringUtils.isEmpty(linkmanId)) {
        Page page = new Page();
        page.getPageList(this.linkmanUpdateHistoryService.getPageLinkmanUpdateHistorysCountByCustomerIdAndLinkmanId(Integer.valueOf(Integer.parseInt(customerId)), Integer.valueOf(Integer.parseInt(linkmanId))).intValue(), Integer.valueOf(5), request);
        map.put("linkmans", this.linkmanUpdateHistoryService.getPageLinkmanUpdateHistorysByCustomerIdAndLinkmanId(Integer.valueOf(Integer.parseInt(customerId)), Integer.valueOf(Integer.parseInt(linkmanId)), page));
      }
    }

    String uId = request.getParameter("uId");
    if (!StringUtils.isEmpty(uId)) {
      map.put("records", this.linkmanService.getLinkmanInfoRecords());
      map.put("ranges", this.linkmanService.getLinkmanDictionaryRanges());
      map.put("linkman", this.linkmanUpdateHistoryService.getLinkmanUpdateHistory(Integer.valueOf(Integer.parseInt(uId))));
    }

    return new ModelAndView(getLinkmanUpdateHistoryDetail(), map);
  }

  public ModelAndView restoreLinkmanInfo(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    String customerId = request.getParameter("customerId");
    map.put("customerId", customerId);
    String sign = request.getParameter("sign");
    map.put("sign", sign);

    String linkmanId = request.getParameter("id");
    map.put("linkmanId", linkmanId);

    boolean result = false;
    int success = 0;
    Integer userId = (Integer)request.getSession().getAttribute("userId");

    String uId = request.getParameter("uId");
    if ((!StringUtils.isEmpty(uId)) && (userId != null)) {
      result = this.linkmanUpdateHistoryService.restoreLinkmanInfo(Integer.valueOf(Integer.parseInt(uId)), userId);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./linkman.do?action=linkmanUpdateHistory&sign=" + sign + "&customerId=" + customerId + "&result=" + success + "&id=" + linkmanId));
  }

  public ModelAndView deptCustomerLinkMan(HttpServletRequest request, HttpServletResponse response, QueryLinkman queryLinkman) {
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    Map model = new HashMap();
    String sFlag = request.getParameter("sFlag");
    String customerName2 = "客户名称";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      customerName2 = request.getParameter("customerName2");
    }
    model.put("customerName2", customerName2);
    model.put("sFlag", sFlag);
    QueryLinkman qLinkman = null;
    if ((!StringUtils.isEmpty(sFlag)) && ("2".equals(sFlag))) {
      qLinkman = queryLinkman;
    }
    model.put("QueryLinkman", qLinkman);

    if ((request.getParameter("see") != null) && (!request.getParameter("see").trim().equals("")) && (request.getParameter("customerId") != null)) {
      String customerId = request.getParameter("customerId");
      String sign = request.getParameter("sign");
      model.put("customerId", customerId);
      model.put("contracts", this.contractService.getContractByCustomerId(Integer.valueOf(Integer.parseInt(customerId))));
      model.put("result", request.getParameter("result"));
      model.put("records", this.customerService.getCustomerInfoRecords());
      model.put("ranges", this.customerService.getCustomerDictionaryRanges());
      model.put("saleStages", this.saleStageSetService.getSaleStages());

      model.put("saleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "0"));
      model.put("hisRootSaleRecords", this.saleRecordService.getCloseSaleRcordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId))));
      model.put("hisSaleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "1"));
      model.put("careSaleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "2"));
      model.put("hangupSaleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "3"));
      String realPath = getServletContext().getRealPath("/");
      if (!StringUtils.isEmpty(customerId)) {
        model.put("customer", this.customerService.getCustomerById2(Integer.valueOf(Integer.parseInt(customerId)), realPath, userId));
        model.put("linkmanRanges", this.linkmanService.getLinkmanDictionaryRanges());
        model.put("saleInfo", this.customerService.getCustomerSaleInfoById(Integer.valueOf(Integer.parseInt(customerId))));

        CustomerLimitForm customerLimitForm = new CustomerLimitForm();
        customerLimitForm = this.customerService.getCustomerLimit(userId);
        if (this.assessmentTypeService.isExpUser(userId)) {
          model.put("isExpUser", "1");
        }
        model.put("customerLimitForm", customerLimitForm);
        model.put("discardLimitNum", this.customerService.getDiscardCustomerNum(userId));

        if ((!StringUtils.isEmpty(sign)) && ("11".equals(sign))) {
          this.customerService.updateViewCustomerDate(Integer.valueOf(Integer.parseInt(customerId)));
        }
      }
    }
    Page page = new Page();
    page.getPageList(this.linkmanService.getDeptCustomerLinkManCount(userId, customerName2, qLinkman).intValue(), Integer.valueOf(10), request);
    model.put("linkmans", this.linkmanService.getDeptCustomerLinkMan(userId, page, customerName2, qLinkman));
    return new ModelAndView(getDeptCustomerLinkMan(), model);
  }

  public ModelAndView ownCustomerLinkMan(HttpServletRequest request, HttpServletResponse response, QueryLinkman queryLinkman) {
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    Map model = new HashMap();
    String sFlag = request.getParameter("sFlag");
    String customerName2 = "客户名称";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      customerName2 = request.getParameter("customerName2");
    }
    model.put("customerName2", customerName2);
    model.put("sFlag", sFlag);
    QueryLinkman qLinkman = null;
    if ((!StringUtils.isEmpty(sFlag)) && ("2".equals(sFlag))) {
      qLinkman = queryLinkman;
    }
    model.put("QueryLinkman", qLinkman);

    if ((request.getParameter("see") != null) && (!request.getParameter("see").trim().equals("")) && (request.getParameter("customerId") != null)) {
      String customerId = request.getParameter("customerId");
      String sign = request.getParameter("sign");
      model.put("customerId", customerId);
      model.put("contracts", this.contractService.getContractByCustomerId(Integer.valueOf(Integer.parseInt(customerId))));
      model.put("result", request.getParameter("result"));
      model.put("records", this.customerService.getCustomerInfoRecords());
      model.put("ranges", this.customerService.getCustomerDictionaryRanges());
      model.put("saleStages", this.saleStageSetService.getSaleStages());

      model.put("saleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "0"));
      model.put("hisRootSaleRecords", this.saleRecordService.getCloseSaleRcordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId))));
      model.put("hisSaleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "1"));
      model.put("careSaleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "2"));
      model.put("hangupSaleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "3"));
      String realPath = getServletContext().getRealPath("/");
      if (!StringUtils.isEmpty(customerId)) {
        model.put("customer", this.customerService.getCustomerById2(Integer.valueOf(Integer.parseInt(customerId)), realPath, userId));
        model.put("linkmanRanges", this.linkmanService.getLinkmanDictionaryRanges());
        model.put("saleInfo", this.customerService.getCustomerSaleInfoById(Integer.valueOf(Integer.parseInt(customerId))));

        CustomerLimitForm customerLimitForm = new CustomerLimitForm();
        customerLimitForm = this.customerService.getCustomerLimit(userId);
        if (this.assessmentTypeService.isExpUser(userId)) {
          model.put("isExpUser", "1");
        }
        model.put("customerLimitForm", customerLimitForm);
        model.put("discardLimitNum", this.customerService.getDiscardCustomerNum(userId));

        if ((!StringUtils.isEmpty(sign)) && ("11".equals(sign))) {
          this.customerService.updateViewCustomerDate(Integer.valueOf(Integer.parseInt(customerId)));
        }
      }
    }
    Page page = new Page();
    page.getPageList(this.linkmanService.getOwnCustomerLinkManCount(userId, customerName2, qLinkman).intValue(), Integer.valueOf(10), request);
    model.put("linkmans", this.linkmanService.getOwnCustomerLinkMan(userId, page, customerName2, qLinkman));
    return new ModelAndView(getOwnCustomerLinkMan(), model);
  }
  public ModelAndView advancedQueryLinkMan(HttpServletRequest request, HttpServletResponse response) {
    Map model = new HashMap();
    model.put("ranges", this.customerService.getCustomerDictionaryRanges());
    return new ModelAndView(getAdvancedQueryLinkMan(), model);
  }

  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    binder.registerCustomEditor(Date.class, new DatePropertyEditor());
  }
}